package com.edu.edumanager.service.imp;


import com.edu.edumanager.bean.EduTeacher;
import com.edu.edumanager.constant.EduConstant;
import com.edu.edumanager.exception.EduException;
import com.edu.edumanager.mapper.EduTeacherMapper;
import com.edu.edumanager.service.TeacherService;
import com.edu.edumanager.utils.EduUploaderUtils;
import com.edu.edumanager.vo.EduResult;
import com.edu.edumanager.vo.EduSubjectList;
import com.edu.edumanager.vo.ElementTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @program: springboot1
 * @description:
 * @author: Jaime
 * @create: 2021-08-01 00:02
 **/
@Service
public class TeacherServiceImp  implements TeacherService {
    @Autowired
    EduTeacherMapper eduTeacherMapper;
    @Override
    public EduTeacher findTeacher(Integer teacherId) {
        EduTeacher eduTeacher =eduTeacherMapper.findTeacher(teacherId);
        return eduTeacher;
    }

    @Override
    public EduResult uploadAvatar(MultipartFile file) {
        //获取图片根路径
        File f = EduUploaderUtils.getRootFolder();
        if(!f.exists()){
            f.mkdirs();
        }
        String imageName  = EduUploaderUtils.getImageName(file.getOriginalFilename());
        try {
            file.transferTo(new File(f,imageName));
        } catch (IOException e) {
            e.printStackTrace();
            throw new EduException("上传图片失败");
        }
        return EduResult.ok().data("imagePath",EduConstant.HOST+EduUploaderUtils.getDateFolder()+"/"+imageName);
    }

    @Override
    public int addTeacher(EduTeacher teacher) {
        //我们查询的时候 一般都要排序的
        teacher.setSort(1);
        Date date = new Date();
        teacher.setGmtCreate(date);
        teacher.setGmtModified(date);
        teacher.setIsDeleted(false);
        int count = eduTeacherMapper.addTeacher(teacher);
        if(count==0){
            throw new EduException("添加讲师失败");

        }
        return count;
    }

    @Override
    public int deleteTeacher(List<Integer> ids) {
        int row = eduTeacherMapper.deleteTeacher(ids);
        if(row ==0){
            throw new EduException("删除讲师失败");
        }
        return row;
    }

    @Override
    public int updateTeacher(EduTeacher eduTeacher) {

        eduTeacher.setGmtModified(new Date());
        int row = eduTeacherMapper.updateTeacher(eduTeacher);
        if(row ==0){
            throw new EduException("修改讲师失败");
        }
        return row;
    }

    @Override
    public EduResult conditionsQuery(Integer currentPage, Integer limit, EduTeacher teacher) {
        List<EduTeacher> rows = eduTeacherMapper.pageQuery((currentPage-1)*limit,limit,teacher);
        int total = eduTeacherMapper.conditionsTotal(teacher);
        return EduResult.ok().data("rows",rows).data("total",total);
    }

    @Override
    public List<EduSubjectList> findTeacherAll() {
        List<EduSubjectList> list = new ArrayList<EduSubjectList>();
        List<EduTeacher>  teacherList =eduTeacherMapper.findTeacherAll();
        for (EduTeacher teacher :teacherList){
            EduSubjectList eduSubjectList = new EduSubjectList();
            eduSubjectList.setValue(teacher.getId());
            eduSubjectList.setLabel(teacher.getName());
            list.add(eduSubjectList);
        }
        return list;
    }
}
